package com.hui.mapper;



import com.hui.dto.SchoolPageDTO;
import com.hui.entity.School;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface SchoolMapper {

    @Insert("""
            insert into rms_school(`title`,`address`,`college`,`info`,`major`,`clazz`)
            values (#{title},#{address},#{college},#{info},#{major},#{clazz})
            """)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert (School school);

    @Select("""
            select * from rms_school t
            where t.id = #{param1} 
            """)
    School select(Long id);

    @Select("""
            <script>
            select * from rms_school t
            <where>
            <if test='title != null'> title like concat('%' , #{title}, '%') and </if>
            t.deleted = 0
            </where>
            </script>
            """)
    List<School> list(SchoolPageDTO schoolPageDTO);

    @Update("""
            <script>
            update rms_school
            <set>
            <if test='title != null'> title = #{title},</if>
            <if test='address != null'> address = #{address},</if>
            <if test='info != null'> info = #{info},</if>
            <if test='major != null'> major = #{major},</if>
            <if test='clazz != null'> clazz = #{clazz},</if>
            <if test='college != null'> college = #{college},</if>
            <if test='updated != null'> updated = #{updated},</if>
            <if test='deleted != null'> deleted = #{deleted},</if>
            <if test='version != null'> version = #{version},</if>
            version = version + 1
            </set>
            where id = #{id} and deleted = 0 and version = #{version} 
            </script>
            """)
    int update(School school);

    @Update("""
            update rms_school
            set deleted = 1, updated = current_timestamp
            where id = #{id} 
            """)
    int delete(Long id);

    @Update("""
            <script>
            update rms_school set deleted = 1, updated = current_timestamp
            where
            <foreach collection='list'  item='e'  open='id in(' close=')' separator=','>
            #{e}
            </foreach>
            </script>
            """)
    int deleteBatch(List<Long> ids);

    @Select("""
            select * from rms_school t
            where t.deleted = 0 and t.title like concat('%' , #{param1}, '%')
            """)
    List<School> listLikeTitle(String title);
}
